Skip to content

[InstCombine] Fold fptrunc(x) ord/uno [ C | fptrunc(y) ] to x ord/uno [ C | y ] - #185844

Merged
Camsyn merged 7 commits into
llvm:mainfrom
Camsyn:fix-185698
Apr 15, 2026
Merged

[InstCombine] Fold fptrunc(x) ord/uno [ C | fptrunc(y) ] to x ord/uno [ C | y ]#185844
Camsyn merged 7 commits into
llvm:mainfrom
Camsyn:fix-185698

Conversation

@Camsyn

@Camsyn Camsyn commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

Recognize TWO new patterns and fold them as follows:

fptrunc(x) ord/uno C           -->  x ord/uno 0
fptrunc(x) ord/uno fptrunc(y)  -->  x ord/uno y

Fixes #185698
Alive2: https://alive2.llvm.org/ce/z/YvXnBJ
IR diff: dtcxzyw/llvm-opt-benchmark#3551
CompTime impact: dtcxzyw/llvm-opt-benchmark#3552

@Camsyn
Camsyn requested a review from nikic as a code owner March 11, 2026 09:46
@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Mar 11, 2026
@llvmbot

llvmbot commented Mar 11, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-llvm-transforms

Author: Kunqiu Chen (Camsyn)

Changes

Recognize TWO new patterns and fold them as follows:

fptrunc(x) ord/uno C           -->  x ord/uno 0
fptrunc(x) ord/uno fptrunc(y)  -->  x ord/uno y

Fixes #185698
Alive2:
IR diff:
CompTime impact:


Full diff: https://github.com/llvm/llvm-project/pull/185844.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp (+42-17)
  • (modified) llvm/test/Transforms/InstCombine/fold-fcmp-trunc.ll (+48-4)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 7f1ced9505b9b..15ff2eb24600c 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -8290,15 +8290,43 @@ static Instruction *foldFCmpReciprocalAndZero(FCmpInst &I, Instruction *LHSI,
 
 // Transform 'fptrunc(x) cmp C' to 'x cmp ext(C)' if possible.
 // Patterns include:
-//    fptrunc(x) <  C  -->  x <  ext(C)
-//    fptrunc(x) <= C  -->  x <= ext(C)
-//    fptrunc(x) >  C  -->  x >  ext(C)
-//    fptrunc(x) >= C  -->  x >= ext(C)
+//    fptrunc(x) <  C                -->  x <  ext(C)
+//    fptrunc(x) <= C                -->  x <= ext(C)
+//    fptrunc(x) >  C                -->  x >  ext(C)
+//    fptrunc(x) >= C                -->  x >= ext(C)
+//    fptrunc(x) ord/uno C           -->  x ord/uno 0
+//    fptrunc(x) ord/uno fptrunc(y)  -->  x ord/uno y
 // where 'ext(C)' is the extension of 'C' to the type of 'x' with a small bias
 // due to precision loss.
 static Instruction *foldFCmpFpTrunc(FCmpInst &I, const Instruction &FPTrunc,
-                                    const Constant &C) {
+                                    const Value &CmpRHS) {
   FCmpInst::Predicate Pred = I.getPredicate();
+  Type *DestType = FPTrunc.getOperand(0)->getType();
+
+  // Handle ord/uno [C | fptrunc(y)]
+  if (Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO) {
+    Value *RHS;
+    const APFloat *CValue;
+    if (match(&CmpRHS, m_FPTrunc(m_Value(RHS)))) {
+      if (DestType != RHS->getType())
+        return nullptr;
+    } else if (match(&CmpRHS, m_APFloat(CValue))) {
+      assert(!CValue->isNaN() &&
+             "NaN RHS should be folded away by simplifyFCmpInst()");
+      RHS = ConstantFP::getZero(DestType);
+    } else {
+      return nullptr;
+    }
+
+    return new FCmpInst(Pred, FPTrunc.getOperand(0), RHS, "", &I);
+  }
+
+  // Handle <, >, <=, >=
+  const APFloat *CValue;
+  // TODO: support vec
+  if (!match(&CmpRHS, m_APFloat(CValue)))
+    return nullptr;
+
   bool RoundDown = false;
 
   if (Pred == FCmpInst::FCMP_OGE || Pred == FCmpInst::FCMP_UGE ||
@@ -8310,10 +8338,6 @@ static Instruction *foldFCmpFpTrunc(FCmpInst &I, const Instruction &FPTrunc,
   else
     return nullptr;
 
-  const APFloat *CValue;
-  if (!match(&C, m_APFloat(CValue)))
-    return nullptr;
-
   if (CValue->isNaN() || CValue->isInfinity())
     return nullptr;
 
@@ -8332,7 +8356,6 @@ static Instruction *foldFCmpFpTrunc(FCmpInst &I, const Instruction &FPTrunc,
 
   APFloat NextCValue = NextValue(*CValue, RoundDown);
 
-  Type *DestType = FPTrunc.getOperand(0)->getType();
   const fltSemantics &DestFltSema =
       DestType->getScalarType()->getFltSemantics();
 
@@ -8354,7 +8377,7 @@ static Instruction *foldFCmpFpTrunc(FCmpInst &I, const Instruction &FPTrunc,
       scalbn(ExtCValue + ExtNextCValue, -1, APFloat::rmNearestTiesToEven);
 
   const fltSemantics &SrcFltSema =
-      C.getType()->getScalarType()->getFltSemantics();
+      CmpRHS.getType()->getScalarType()->getFltSemantics();
 
   // 'MidValue' might be rounded to 'NextCValue'. Correct it here.
   APFloat MidValue = ConvertFltSema(ExtMidValue, SrcFltSema);
@@ -8835,10 +8858,16 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
     }
   }
 
-  // Handle fcmp with instruction LHS and constant RHS.
   Instruction *LHSI;
+  // Handle fcmp with fptrunc LHS
+  if (match(Op0, m_Instruction(LHSI)) &&
+      LHSI->getOpcode() == Instruction::FPTrunc)
+    if (Instruction *NV = foldFCmpFpTrunc(I, *LHSI, *Op1))
+      return NV;
+
+  // Handle fcmp with instruction LHS and constant RHS.
   Constant *RHSC;
-  if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) {
+  if (LHSI && match(Op1, m_Constant(RHSC))) {
     switch (LHSI->getOpcode()) {
     case Instruction::Select:
       // fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
@@ -8872,10 +8901,6 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
                 foldCmpLoadFromIndexedGlobal(cast<LoadInst>(LHSI), GEP, I))
           return Res;
       break;
-    case Instruction::FPTrunc:
-      if (Instruction *NV = foldFCmpFpTrunc(I, *LHSI, *RHSC))
-        return NV;
-      break;
     }
   }
 
diff --git a/llvm/test/Transforms/InstCombine/fold-fcmp-trunc.ll b/llvm/test/Transforms/InstCombine/fold-fcmp-trunc.ll
index 371f9b6807fe4..e65e31700240f 100644
--- a/llvm/test/Transforms/InstCombine/fold-fcmp-trunc.ll
+++ b/llvm/test/Transforms/InstCombine/fold-fcmp-trunc.ll
@@ -57,6 +57,52 @@ define i1 @fcmp_trunc_zero(double %0) {
   ret i1 %result
 }
 
+define i1 @fcmp_trunc_uno(double %0) {
+; CHECK-LABEL: define i1 @fcmp_trunc_uno(
+; CHECK-SAME: double [[TMP0:%.*]]) {
+; CHECK-NEXT:    [[RESULT:%.*]] = fcmp uno double [[TMP0]], 0.000000e+00
+; CHECK-NEXT:    ret i1 [[RESULT]]
+;
+  %trunc = fptrunc double %0 to float
+  %result = fcmp uno float %trunc, 0.000000e+00
+  ret i1 %result
+}
+
+define i1 @fcmp_trunc_ord_inf(double %0) {
+; CHECK-LABEL: define i1 @fcmp_trunc_ord_inf(
+; CHECK-SAME: double [[TMP0:%.*]]) {
+; CHECK-NEXT:    [[RESULT:%.*]] = fcmp ord double [[TMP0]], 0.000000e+00
+; CHECK-NEXT:    ret i1 [[RESULT]]
+;
+  %trunc = fptrunc double %0 to float
+  %result = fcmp ord float %trunc, 0x7FF0000000000000
+  ret i1 %result
+}
+
+define i1 @fcmp_trunc_pair_uno(double %x, double %y) {
+; CHECK-LABEL: define i1 @fcmp_trunc_pair_uno(
+; CHECK-SAME: double [[X:%.*]], double [[Y:%.*]]) {
+; CHECK-NEXT:    [[RESULT:%.*]] = fcmp uno double [[X]], [[Y]]
+; CHECK-NEXT:    ret i1 [[RESULT]]
+;
+  %tx = fptrunc double %x to float
+  %ty = fptrunc double %y to float
+  %result = fcmp uno float %tx, %ty
+  ret i1 %result
+}
+
+define i1 @fcmp_trunc_pair_ord(double %x, double %y) {
+; CHECK-LABEL: define i1 @fcmp_trunc_pair_ord(
+; CHECK-SAME: double [[X:%.*]], double [[Y:%.*]]) {
+; CHECK-NEXT:    [[RESULT:%.*]] = fcmp ord double [[X]], [[Y]]
+; CHECK-NEXT:    ret i1 [[RESULT]]
+;
+  %tx = fptrunc double %x to float
+  %ty = fptrunc double %y to float
+  %result = fcmp ord float %tx, %ty
+  ret i1 %result
+}
+
 define i1 @fcmp_trunc_with_nnan(double %0) {
 ; CHECK-LABEL: define i1 @fcmp_trunc_with_nnan(
 ; CHECK-SAME: double [[TMP0:%.*]]) {
@@ -104,7 +150,7 @@ define i1 @fcmp_trunc_with_reassoc(double %0) {
 define i1 @fcmp_trunc_with_fast(double %0) {
 ; CHECK-LABEL: define i1 @fcmp_trunc_with_fast(
 ; CHECK-SAME: double [[TMP0:%.*]]) {
-; CHECK-NEXT:    [[RESULT:%.*]] = fcmp fast oge double [[TMP0]], 0x4058FFFFF0000000 
+; CHECK-NEXT:    [[RESULT:%.*]] = fcmp fast oge double [[TMP0]], 0x4058FFFFF0000000
 ; CHECK-NEXT:    ret i1 [[RESULT]]
 ;
   %trunc = fptrunc double %0 to float
@@ -327,8 +373,7 @@ define i1 @fcmp_trunc_literal_positive_inf(double %0) {
 define i1 @fcmp_trunc_literal_negative_inf(double %0) {
 ; CHECK-LABEL: define i1 @fcmp_trunc_literal_negative_inf(
 ; CHECK-SAME: double [[TMP0:%.*]]) {
-; CHECK-NEXT:    [[TRUNC:%.*]] = fptrunc double [[TMP0]] to float
-; CHECK-NEXT:    [[RESULT:%.*]] = fcmp uno float [[TRUNC]], 0.000000e+00
+; CHECK-NEXT:    [[RESULT:%.*]] = fcmp uno double [[TMP0]], 0.000000e+00
 ; CHECK-NEXT:    ret i1 [[RESULT]]
 ;
   %trunc = fptrunc double %0 to float
@@ -671,4 +716,3 @@ define i1 @fcmp_trunc_mn_ppc_fp128(ppc_fp128 %0) {
   %result = fcmp uge float %trunc, 0xC7EFFFFF00000000
   ret i1 %result
 }
-

@Camsyn
Camsyn requested a review from dtcxzyw March 11, 2026 11:59
@Camsyn Camsyn changed the title [InstCombine] Fold fptrunc(x) ord/uno [ C | fptrunc(y) ] [InstCombine] Fold fptrunc(x) ord/uno [ C | fptrunc(y) ] to x ord/uno [ C | y ] Mar 11, 2026
Comment thread llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp Outdated
Comment thread llvm/test/Transforms/InstCombine/fold-fcmp-trunc.ll
@github-actions

github-actions Bot commented Apr 13, 2026

Copy link
Copy Markdown

🪟 Windows x64 Test Results

  • 133507 tests passed
  • 3096 tests skipped

✅ The build succeeded and all tests passed.

@github-actions

github-actions Bot commented Apr 13, 2026

Copy link
Copy Markdown

🐧 Linux x64 Test Results

  • 193588 tests passed
  • 5044 tests skipped

✅ The build succeeded and all tests passed.

@dtcxzyw dtcxzyw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Camsyn
Camsyn merged commit 120cbbd into llvm:main Apr 15, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missed optimization: fold fcmp uno/ord of fptrunc into fcmp of original operand

3 participants